package fr.umlv.m2.recipes.ideas;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;

/**
 * Create the Favorite Activity view in the tab widget
 * @author Christophe Chillan
 *
 */
public class FavoriteWidget extends Activity {
	
	private ListView favoriteListView;
	/**
	 * {@inheritDoc} 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.favorite);
	    createFavoriteListView();
	}

	private void createFavoriteListView() {
		favoriteListView = (ListView) findViewById(R.id.listviewperso);
		
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		final List<RecipeBean> recipes = RecipesDB.getRecipeDao().getFavoritesRecipes(RecipesDB.getDatabase());
		for(RecipeBean recipe : recipes){
			map = new HashMap<String, String>();
			map.put("titre", recipe.getName());
			map.put("description", recipe.getName());
			map.put("img",String.valueOf(getResources().getIdentifier(recipe.getPictureLink()+"_liste", null, getPackageName())));
			listItem.add(map);
		}
		
		//Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue affichageitem
		SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.recipeslist,
				new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
		favoriteListView.setAdapter(mSchedule);
		
		favoriteListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position, long id) {

				RecipeBean recipe = recipes.get(position);
				Bundle bundle = new Bundle();
				bundle.putSerializable("recipe", recipe);

				Intent intent = new Intent(FavoriteWidget.this.getApplicationContext(), MenuTabIngredient.class);
				intent.putExtras(bundle);
				startActivity(intent);

			}
		});
	}
	
	/**
	 * Use to sort the list with Alphabetic order
	 * @param target
	 */
	public void sortAlpha(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getFavoritesRecipesOrderedByName(RecipesDB.getDatabase());
		makeSortedButton(recipes);
		
	}
	
	/**
	 * Use to sort the list in Difficulty order
	 * @param target
	 */
	public void sortDifficulty(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getFavoritesRecipesOrderedByDifficulty(RecipesDB.getDatabase());
		makeSortedButton(recipes);
		
	}
	
	/**
	 * Use to sort the list with Price order
	 * @param target
	 */
	public void sortPrice(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getFavoriteRecipesOrderedByPrice(RecipesDB.getDatabase());
		makeSortedButton(recipes);
	}
	
	/**
	 * Use to sort the list with the evaluation order
	 * @param target
	 */
	public void sortVote(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getFavoritesRecipesOrderedByVote(RecipesDB.getDatabase());
		makeSortedButton(recipes);
	}
	
	
	private void makeSortedButton(List<RecipeBean> recipes){
		recipes = new ArrayList<RecipeBean>(recipes);
		Bundle bundle = new Bundle();
		bundle.putSerializable("recipes", new ArrayList<RecipeBean>(recipes));
		Intent intent = new Intent(getApplicationContext(), MenuTabWidget.class);
		intent.putExtras(bundle);
		intent.putExtra("indexTab", 2);
		startActivity(intent);	
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}
}
